/**
 * Copyright (c) 2011 Tomi Paananen.
 *
 * Part of Quick Gestures engine library.
 */

// Own header
#include "gesture.h"

// Constants
const QString UntitledGestureName("Untitled");


/*!
  \class Gesture
  \brief Representation of a gesture.
*/


/*!
  Constructor.
*/
Gesture::Gesture(QObject *parent /* = 0 */)
: QObject(parent),
  m_id(qrand() /* % RAND_MAX */),
  m_name(UntitledGestureName),
  m_attributes(None)
{
}


/*!
  Copy constructor.
*/
Gesture::Gesture(const Gesture &gesture, QObject *parent /* = 0 */)
: QObject(parent),
  m_id(gesture.m_id),
  m_name(gesture.m_name),
  m_attributes(gesture.m_attributes),
  m_points(gesture.m_points)
{
}


/*!
  Destructor.
*/
Gesture::~Gesture()
{
}


/*!
  Assignment operator.
*/
Gesture &Gesture::operator=(const Gesture &gesture)
{
    if (this != &gesture) {
        m_id = gesture.m_id;
        m_attributes = gesture.m_attributes;
        m_name = gesture.m_name;
        m_points = gesture.m_points;
    }

    return *this;
}


/*!
  Comparison operator.
  Note that the ID does not affect the comparison.
*/
bool Gesture::operator==(const Gesture &gesture)
{
    if (m_name.compare(gesture.m_name) ||
        m_attributes != gesture.m_attributes) {
        return false;
    }
    
    const int count(m_points.count());
    
    if (count != gesture.m_points.count()) {
        return false;
    }
    
    for (int i = 0; i < count; ++i) {
        if (m_points[i] != gesture.m_points[i]) {
            return false;
        }
    }
    
    return true;
}


/*!
  Comparison operator.
  Note that the ID does not affect the comparison.
*/
bool Gesture::operator!=(const Gesture &gesture)
{
    if (m_name.compare(gesture.m_name) ||
        m_attributes != gesture.m_attributes) {
        return true;
    }
    
    const int count(m_points.count());
    
    if (count != gesture.m_points.count()) {
        return true;
    }
    
    for (int i = 0; i < count; ++i) {
        if (m_points[i] != gesture.m_points[i]) {
            return true;
        }
    }
    
    return false;
}


/*!
  Sets \a id as the gesture ID.
*/
void Gesture::setId(quint32 id)
{
    m_id = id;
    emit idChanged(m_id);
}


/*!
  Returns the gesture ID.
*/
quint32 Gesture::id() const
{
    return m_id;
}


/*!
  Returns the name of the gesture.
*/
const QString &Gesture::name() const
{
    return m_name;
}


/*!
  Returns the gesture attributes.
*/
quint32 Gesture::attributes() const
{
    return m_attributes;
}


/*!
  Returns the gesture point count.
*/
int Gesture::pointCount() const
{
    return m_points.count();
}


/*!
  Returns the gesture points.
*/
const PointList &Gesture::points() const
{
    return m_points;
}


/*!
  Returns the gesture points that can be modified.
*/
PointList &Gesture::modifiablePoints()
{
    return m_points;
}


/*!
  Sets \a name as the name of the gesture.
*/
void Gesture::setName(const QString &name)
{
    m_name = name;
    emit nameChanged(m_name);
}


/*!
  Sets the gesture attributes according to \a attributes.
*/
void Gesture::setAttributes(quint32 attributes)
{
    m_attributes = attributes;
    emit attributesChanged(m_attributes);
}


/*!
  Sets \a points as the gesture points.
*/
void Gesture::setPoints(PointList &points)
{
    m_points = points;
    emit pointCountChanged(m_points.count());
}


/*!
  Appends \a point to the list of points.
*/
void Gesture::appendPoint(QPoint point)
{
    if (!m_points.count() && point == QGEngine::BreakPoint) {
        // Do not append a break point as the first point of the gesture.
        return;
    }

    m_points.append(point);
    emit pointCountChanged(m_points.count());
}


/*!
  For convenience. Appends a point with coordinates \a x and \a y to the list
  of points.
*/
void Gesture::appendPoint(int x, int y)
{
    appendPoint(QPoint(x, y));
}


/*!
  Clears the points.
*/
void Gesture::clear()
{
    if (m_points.count()) {
        m_points.clear();
        emit pointCountChanged(0);
    }
}


// End of file.    
